var app = getApp();
var md5 = require('../pages/md5.js');
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function toAiIndex() {
  var exhibitionid = wx.getStorageSync('exhibitionid')
  var u = wx.getStorageSync('u')
  if(exhibitionid=='0' && u=='0'){
    wx.showToast({
      title: '请登录！',
      icon: 'none',
      duration: 2000
    })
  }else{
    wx.reLaunch({
      url: '../aiIndex/aiIndex'
    })
  }

}

function toContactsIng() {
  var exhibitionid = wx.getStorageSync('exhibitionid')
  var u = wx.getStorageSync('u')
  if(exhibitionid=='0' && u=='0'){
    wx.showToast({
      title: '请登录！',
      icon: 'none',
      duration: 2000
    })
  }else{
  //获取状态
  var exhibitionid = wx.getStorageSync('exhibitionid')
  var u = wx.getStorageSync('u')
  var state = '1'
  console.log('exhibitionid=' + exhibitionid)
  console.log('u=' + u)
  if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid')) &&
    (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
    //默认最新（调用pico接口）
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/user/setrecommend'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["u"] = u
    k["opstate"] = '0'
    var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'opstate0' + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.hexMD5(sign)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        var curdata = mesg.data
        if (mesg.code == 200) {
          var state = curdata.state
          console.log('state=' + state)
          if (state == '1') {
            wx.reLaunch({
              url: '../contactsModule/contactsIng'
            })
          }
          if (state == '0') {
            wx.reLaunch({
              url: '../contactsModule/maillist'
            })
          }
        } else {
          wx.reLaunch({
            url: '../contactsModule/contactsIng'
          })
        }
      },
      fail: res => {
        console.log(res, "error")
      }

    })
  } else {
    wx.redirectTo({
      url: '../signInbyPhone/signIn'
    })
  }
 }

}

function toProductRevision() {
  var exhibitionid = wx.getStorageSync('exhibitionid')
  var u = wx.getStorageSync('u')
  if(exhibitionid=='0' && u=='0'){
    wx.showToast({
      title: '请登录！',
      icon: 'none',
      duration: 2000
    })
  }else{
    wx.reLaunch({
      url: '../dataModule/productRevision'
    })
 }
}

function toMy() {
  var exhibitionid = wx.getStorageSync('exhibitionid')
  var u = wx.getStorageSync('u')
  if(exhibitionid=='0' && u=='0'){
    wx.showToast({
      title: '请登录！',
      icon: 'none',
      duration: 2000
    })
  }else{
    wx.reLaunch({
      url: '../my/my'
    })
 }
}

function toBadge() {
  var exhibitionid = wx.getStorageSync('exhibitionid')
  var u = wx.getStorageSync('u')
  if(exhibitionid=='0' && u=='0'){
    wx.showToast({
      title: '请登录！',
      icon: 'none',
      duration: 2000
    })
  }else{
    wx.reLaunch({
      url: '../badge/Badge'
    })
 }
}

module.exports = {
  formatTime: formatTime,
  toAiIndex: toAiIndex,
  toContactsIng: toContactsIng,
  toProductRevision: toProductRevision,
  toMy: toMy,
  toBadge: toBadge

}